package com.lanhai.zhonglai.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lanhai.zhonglai.utils.Tools;
import com.lanhai.zhonglai.beans.DotBean;
import com.lanhai.zhonglai.dao.DotDao;
import com.lanhai.zhonglai.dao.RelQrAndTransDao;
import com.lanhai.zhonglai.enums.DotTypeEnum;
import com.lanhai.zhonglai.interfaces.IDot;
import com.lanhai.zhonglai.mapper.DotMapper;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Service
public class DotImpl implements IDot {
    @Autowired
    JedisPool jedisPool;

    @Autowired
    DotMapper dotMapper;

    static List<DotDao> list = new ArrayList<DotDao>();
    static List<RelQrAndTransDao> qrlist = new ArrayList<RelQrAndTransDao>();


    static Map<Integer, Integer> dotForAgvMap = new ConcurrentHashMap<Integer, Integer>();

    /**
     * 获取点位
     */
    @Override
    public synchronized DotBean getBean(int dotno) {

        DotBean bean = null;
        for (DotDao dao : list) {
            if (dao.getDotno() == dotno) {
                bean = new DotBean();
                bean.setArea(dao.getX(), dao.getY(), 30);
                bean.setDotName(dao.getDotname());
                bean.setDotno(dotno);
                bean.setDotType(DotTypeEnum.getDotTypeEnum(dao.getPtype()));
                bean.setRouteRoad(dao.getRouteroad());
                bean.setNextProcess(dao.getNextProcess());
            }
        }
        return bean;
    }

    /**
     *
     */
    @Override
    public synchronized DotBean getBeanByXY(int x, int y) {
        int distance = 30;

        DotBean bean = null;
        for (DotDao dao : list) {
            if (Math.abs(dao.getX() - x) <= distance && Math.abs(dao.getY() - y) <= distance) {
                bean = new DotBean();
                bean.setArea(dao.getX(), dao.getY(), distance);
                bean.setDotName(dao.getDotname());
                bean.setDotno(dao.getDotno());
                bean.setDotType(DotTypeEnum.getDotTypeEnum(dao.getPtype()));
                bean.setRouteRoad(dao.getRouteroad());
                bean.setNextProcess(dao.getNextProcess());
            }
        }
        return bean;
    }


    @Override
    public int getTransDotNo(int qrdotno) {
        int p = 0;
        for (RelQrAndTransDao qr : qrlist) {
            if (qr.getQr() == qrdotno) {
                p = qr.getTrans();
                break;
            }
        }
        return p;
    }

    @Override
    public void init() {
        List<DotDao> tmp = dotMapper.getDotList();
        list.clear();
        if (tmp != null && tmp.size() > 0) {
            list.addAll(tmp);
        }

        List<RelQrAndTransDao> qlist = dotMapper.getQrTransList();
        qrlist.clear();
        if (qlist != null && qlist.size() > 0) {
            qrlist.addAll(qlist);
        }


    }


    @Override
    public synchronized void setDotOfAgv(int dotNo, int agvid) {

        String mainkey = "dotofAgv";
        Jedis jd = jedisPool.getResource();

        if (agvid > 0) {
            // 先移除小车曾经可能的点位（人工操作的小车）
            for (Entry<Integer, Integer> entry : dotForAgvMap.entrySet()) {
                if (entry.getValue() == agvid) {
                    entry.setValue(0);
                    jd.hset(mainkey, "" + entry.getKey(), "0");
                }
            }
        }
        // 添加现有的
        dotForAgvMap.put(dotNo, agvid);
        jd.hset(mainkey, "" + dotNo, "" + agvid);
        jd.close();

    }

    @Override
    public int getAgvIdByDotNo(int dotNo) {
        Integer agvid = dotForAgvMap.get(dotNo);
        if (agvid == null) {
            // 不存在，从redis中拿
            String mainkey = "dotofAgv";
            String key = String.format("%d", dotNo);
            Jedis jd = jedisPool.getResource();
            String m = jd.hget(mainkey, key);
            jd.close();

            if (m != null && m.isEmpty() == false) {
                agvid = Tools.strToInt(m);
                dotForAgvMap.put(dotNo, agvid);
                return agvid;
            } else {
                return 0;
            }
        } else {
            return agvid;
        }
    }

    @Override
    public int getQrDotNo(int transdotno) {
        int p = 0;
        for (RelQrAndTransDao qr : qrlist) {
            if (qr.getTrans() == transdotno) {
                p = qr.getQr();
                break;
            }
        }
        return p;
    }

    @Override
    public boolean isNearTransDot(int transdot, int x, int y) {
        boolean flag = false;
        for (DotDao dao : list) {
            //找到点位，如果在2米开外，返回true
            if (dao.getDotno() == transdot) {
                if (Math.abs(dao.getX() - x) <= 200 && Math.abs(dao.getY() - y) <= 200) {
                    flag = true;
                }
                break;
            }
        }
        return flag;
    }

}
